package com.example.demo.leetcode.top100;

import java.util.*;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _18矩阵置零
 * @description
 * @date 2023/06/07 21:22
 * <p>
 * ******************************************************
 */
public class _18矩阵置零 {

    public void setZeroes(int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;
        Set<Coordinate> zeroSet = new HashSet<>();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (matrix[i][j] == 0) {
                    // 记录坐标
                    addZeroSet(zeroSet, i, j, m, n);
                }
            }
        }

        for (Coordinate coordinate : zeroSet) {
            matrix[coordinate.getX()][coordinate.getY()] = 0;
        }
    }

    private void addZeroSet(Set<Coordinate> zeroSet, int i, int j, int m, int n) {
        for (int k = 0; k < m; k++) {
            zeroSet.add(new Coordinate(k, j));
        }

        for (int l = 0; l < n; l++) {
            zeroSet.add(new Coordinate(i, l));
        }
    }

    public class Coordinate {
        private int x;
        private int y;

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }

        public Coordinate(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Coordinate that = (Coordinate) o;
            return x == that.x && y == that.y;
        }

        @Override
        public int hashCode() {
            return Objects.hash(x, y);
        }
    }

}
